package com.zlc.common.util.context;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zlc.common.annotation.Parameter;
import com.zlc.common.annotation.SyncParameterConstant;
import com.zlc.common.common.constant.ParameterType;
import com.zlc.common.common.service.ContextHolder;
import com.zlc.common.core.sys.model.vo.MayanPlatParameterVo;
import com.zlc.common.core.sys.service.MayanPlatParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class SysParameterInit {

    private ApplicationContext context;

    @Autowired
    private MayanPlatParameterService mayanPlatParameterService;

    public void initParameter(ApplicationContext context) {
        this.context = context;

        Map<String, Object> parameterBeans = context.getBeansWithAnnotation(SyncParameterConstant.class);
        ContextHolder.setDefaultUser();
        //查询所有系统参数
        List<MayanPlatParameterVo> allParameters = mayanPlatParameterService.queryVoList(new LambdaQueryWrapper<>());

        //需要保存的参数
        List<MayanPlatParameterVo> saveList = new ArrayList<>();
        for (Object parameterBean : parameterBeans.values()) {
            Field[] fields = parameterBean.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                Parameter annotation = field.getAnnotation(Parameter.class);
                if (annotation != null) {
                    ParameterType type = annotation.type();
                    String value = annotation.defaultValue();
                    String desc = annotation.desc();
                    String catalog = annotation.catalog();
                    int sortNum = annotation.sortNum();

                    Object key = null;
                    try {
                        key = field.get(parameterBean);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    if (ObjectUtils.isEmpty(key)) {
                        continue;
                    }

                    value = value == null ? "" : value;
                    //查询数据库中是否存在
                    Object finalKey = key;
                    MayanPlatParameterVo existParameter = allParameters.stream().filter(a -> a.getParameterKey().equals(finalKey.toString()))
                            .findFirst().orElse(null);
                    if (!ObjectUtils.isEmpty(existParameter)) {
                        //发生更新了,做修改
                        if (!existParameter.getType().equals(type.name()) || !existParameter.getDescription().equals(desc)
                                || !existParameter.getCatalog().equals(catalog) || (existParameter.getSortNum() != null && existParameter.getSortNum() != sortNum)) {
                            existParameter.setType(type.name());
                            existParameter.setCatalog(catalog);
                            existParameter.setDescription(desc);
                            existParameter.setSortNum(sortNum);
                            saveList.add(existParameter);
                        }
                    } else {
                        MayanPlatParameterVo parameterVo = new MayanPlatParameterVo();
                        parameterVo.setType(type.name());
                        parameterVo.setCatalog(catalog);
                        parameterVo.setDescription(desc);
                        parameterVo.setDefaultValue(value);
                        parameterVo.setSortNum(sortNum);
                        parameterVo.setParameterKey(key.toString());
                        saveList.add(parameterVo);
                    }
                }
            }
        }
        if(!ObjectUtils.isEmpty(saveList)) {
            mayanPlatParameterService.saveBatchVoList(saveList);
        }
    }
}
